<div id="sidebar">
	<!-- 显示友情链接模块 -->
	<?php
		$query_link = "select * from `links`";
		$result_link = $mysql_connection->query($query_link);
	?>
	<div class="sidebar_div">
		<div class="sidebar_div_title">友情链接</div>
		<div class="sidebar_div_content">
			<?php
				if ($setting['show_link_sidebar']=='zhengchang_select_link') {
				?>
				<ul>
					<?php
						while($each_link=$result_link->fetch_assoc()) {
						?>
						<li><a href="<?php echo $each_link['url'];?>" title="<?php echo $each_link['miaoshu'];?>" target="<?php echo $each_link['open_type'];?>"><?php echo $each_link['name'];?></a></li>
						<?php
						}
					?>
				</ul>
				<?php
				} else if ($setting['show_link_sidebar']=='xialaliebiao_select_link') {
				?>
				<select class="width_select_sidebar" onchange='document.location.href=this.options[this.selectedIndex].value;'>
					<option>友情链接</option>
					<?php
						while($each_link=$result_link->fetch_assoc()) {
						?>
						<option value="<?php echo $each_link['url'];?>"><?php echo $each_link['name'];?></option>
						<?php
						}
					?>
				</select>
				<?php
				} else if ($setting['show_link_sidebar']=='gundong_select_link') {
				?>
				<marquee scrollAmount="1" height="50px" direction="up" onMouseOver="this.stop()" onMouseOut="this.start()"><ul>
					<?php
						while($each_link=$result_link->fetch_assoc()) {
						?>
						<li><a href="<?php echo $each_link['url'];?>" title="<?php echo $each_link['miaoshu'];?>" target="<?php echo $each_link['open_type'];?>"><?php echo $each_link['name'];?></a></marquee></li>
						<?php
						}
					?>
				</ul></marquee>
				<?php
				}
			?>
		</div>
	</div>
	<!-- 显示友情链接模块 END -->


	<?php
		// 取得最新评论及其相关信息
		$query_news_comments = "select * from `comment` order by `post_time` desc limit ".$setting['sidebar_num_comment'];
		$result_news_comments = $mysql_connection->query($query_news_comments);
		$num_news_comments = $result_news_comments->num_rows;
		$arr_new_comment = array();
		for ($i=0; $i<$num_news_comments; $i+=1) {
			$each_comment = $result_news_comments->fetch_assoc();
			// 取得评论所属帖子的标题
			$id_article_sidebar = $each_comment['articles_id'];
			$query_title_article_sidebar = "select `title` from `articles` where `id`=".$id_article_sidebar;
			$result_title_article_sidebar = $mysql_connection->query($query_title_article_sidebar);
			$title_article_sidebar = $result_title_article_sidebar->fetch_assoc();
			$title_article_sidebar = $title_article_sidebar['title'];
			// 取得评论所属帖子的标题 END
			$arr_new_comment[$each_comment['id']] = array(
				'nickname'=>$each_comment['nickname'],
				'id_article_sidebar'=>$id_article_sidebar,
				'title_article_sidebar'=>$title_article_sidebar
			);
		}
		// 取得最新评论及其相关信息 END
	?>
	<!-- start 显示最新评论 -->
	<div class="sidebar_div">
		<div class="sidebar_div_title">最新评论</div>
		<div class="sidebar_div_content">
			<?php
				if ($setting['show_comment_sidebar']=='zhengchang_select_comment') {
				?>
				<ul><?php
					foreach ($arr_new_comment as $comment_id => $arr_comm) {
						echo '<li>'.$arr_comm['nickname'].' 发表于 ';
						echo '《<a href="article.php?aid='.$arr_comm['id_article_sidebar'].'#'.$comment_id.'-comment">'.$arr_comm['title_article_sidebar'].'</a>》</li>';
					}
				?></ul>
				<?php
				} else if ($setting['show_comment_sidebar']=='gundong_select_comment') {
				?>
				<marquee scrollAmount="1" height="50px" direction="up" onMouseOver="this.stop()" onMouseOut="this.start()"><ul><?php
					foreach ($arr_new_comment as $comment_id => $arr_comm) {
						echo '<li>'.$arr_comm['nickname'].' 发表于 ';
						echo '《<a href="article.php?aid='.$arr_comm['id_article_sidebar'].'#'.$comment_id.'-comment">'.$arr_comm['title_article_sidebar'].'</a>》</li>';
					}
				?></ul></marquee>
				<?php
				}
			?>
		</div>
	</div>
	<!-- end 显示最新评论 -->

	<!-- start 显示帖子分类 -->
	<div class="sidebar_div">
		<div class="sidebar_div_title">帖子分类</div>
		<div class="sidebar_div_content"><?php
			$query_category_sidebar = "select * from `category` order by `name` asc";
			$result_category_sidebar = $mysql_connection->query($query_category_sidebar);
			$count_category_sidebar = $result_category_sidebar->num_rows;
			$arr_category_sidebar = array();
			for ( $i=0; $i<$count_category_sidebar; $i+=1 ) {
				$each_category_sidebar = $result_category_sidebar->fetch_assoc();
				$count_article_category = get_count_article_category($each_category_sidebar['id'],$mysql_connection);
				$arr_category_sidebar[$each_category_sidebar['id']]=array(
					'name_category'=>$each_category_sidebar['name'],
					'count_article_category'=>$count_article_category
				);
			}
			if ($setting['show_category_sidebar']=='zhengchang_select_category') {
				foreach($arr_category_sidebar as $id_category_arr=>$arr_category) {
					echo '<div><a href="index.php?cat='.$id_category_arr.'">'.$arr_category['name_category'].'&nbsp;('.$arr_category['count_article_category'].')</a></div>';
				}
			} else if ($setting['show_category_sidebar']=='xialaliebiao_select_category') {
			?>
			<select class="width_select_sidebar" onchange='document.location.href=this.options[this.selectedIndex].value;'>
			<option>帖子分类</option>
			<?php
				foreach($arr_category_sidebar as $id_category_arr => $arr_category) {
					echo '<option value="index.php?cat='.$id_category_arr.'">'.$arr_category['name_category'].'&nbsp;('.$arr_category['count_article_category'].')</option>';
				}
			?></select>
			<?php
			}
		?></div>
	</div>
	<!-- end 显示帖子分类 -->

	<!-- start 显示帖子存档 -->
	<div class="sidebar_div">
		<div class="sidebar_div_title">存档</div>
		<div class="sidebar_div_content">
		<?php
			// start 取得时间最晚的帖子的时间-年份和月份
			$query_archive ="select date_format(`post_time`,'%m') as month,date_format(`post_time`,'%Y') as year from `articles` order by `post_time` desc limit 1";
			$result_query_archive = $mysql_connection->query($query_archive);
			$top_post_time = $result_query_archive->fetch_assoc();
			// end 取得时间最晚的帖子的时间-年份和月份
			// start 取得时间最早的帖子的时间-年份和月份
			$query_archive = "select date_format(`post_time`,'%Y') as year, date_format(`post_time`, '%m') as month from `articles` order by `post_time` asc limit 1";
			$result_query_archive = $mysql_connection->query($query_archive);
			$bottom_post_time = $result_query_archive->fetch_assoc();
			// end 取得时间最早的帖子的时间-年份和月份
			$array_cundang = array();
			// 把存档数据放入一个数组
			if ((!isset($bottom_post_time)) or (!isset($top_post_time))) {
				echo '暂无文章';
			} else {
				for ($i=$top_post_time['year']; $i>=$bottom_post_time['year']; $i-=1) {
					if ($i==$top_post_time['year'] and $top_post_time['year'] == $bottom_post_time['year']) {
						for ($j=$top_post_time['month']; $j>=$bottom_post_time['month']; $j-=1) {
							$j = sprintf('%02d',$j); //在月份前面加零
							$count_article_date = get_count_article_date($j,$i,$mysql_connection);
							if ($count_article_date>0) {
								$array_cundang[] = array('month'=>$j,'year'=>$i,'count'=>$count_article_date);
							}
						}
					} else if ($i == $top_post_time['year']) {
						for ($j=$top_post_time['month']; $j>=1; $j-=1) {
							$j = sprintf('%02d',$j); //在月份前面加零
							$count_article_date = get_count_article_date($j,$i,$mysql_connection);
							if ($count_article_date>0) {
								$array_cundang[] = array('month'=>$j,'year'=>$i,'count'=>$count_article_date);
							}
						}
					} else if ($i == $bottom_post_time['year']) {
						for ($j=12; $j>=$bottom_post_time['month']; $j-=1) {
							$j = sprintf('%02d',$j); //在月份前面加零
							$count_article_date = get_count_article_date($j,$i,$mysql_connection);
							if ($count_article_date>0) {
								$array_cundang[] = array('month'=>$j,'year'=>$i,'count'=>$count_article_date);
							}
						}
					} else {
						for ($j=12; $j>=1; $j-=1) {
							$j = sprintf('%02d',$j); //在月份前面加零
							$count_article_date = get_count_article_date($j,$i,$mysql_connection);
							if ($count_article_date>0) {
								$array_cundang[] = array('month'=>$j,'year'=>$i,'count'=>$count_article_date);
							}
						}
					}
				}
			}
			// 把存档数据放入一个数组 END
			// 显示存档在侧栏
			if ($setting['show_cundang_sidebar']=='zhengchang_select_cundang') {
				foreach ($array_cundang as $key => $array_each_cundang) {
				?>
				<a href="index.php?m=<?php echo $array_each_cundang['month'];?>&y=<?php echo $array_each_cundang['year'];?>">
					<?php echo $array_each_cundang['year'];?>&nbsp;年&nbsp;<?php echo $array_each_cundang['month'];?>&nbsp;月&nbsp;(<?php echo $array_each_cundang['count'];?>)
				</a>
				<?php
				}
			} else if ($setting['show_cundang_sidebar']=='xialaliebiao_select_cundang') {
			?>
			<select class="width_select_sidebar" onchange='document.location.href=this.options[this.selectedIndex].value;'>
			<option>帖子存档</option>
			<?php
				foreach ($array_cundang as $key => $array_each_cundang) {
				?>
				<option value="index.php?m=<?php echo $array_each_cundang['month'];?>&y=<?php echo $array_each_cundang['year'];?>">
					<?php echo $array_each_cundang['year'];?>&nbsp;年&nbsp;<?php echo $array_each_cundang['month'];?>&nbsp;月&nbsp;(<?php echo $array_each_cundang['count'];?>)
				</option>
				<?php
				}
			?>
			</select>
			<?php
			}
			// 显示存档侧栏 END
		?>
		</div>
	</div>
	<!-- end 显示帖子存档 -->

	<!-- start rss 订阅 -->
	<div class="sidebar_div">
		<div class="sidebar_div_title">rss 订阅</div>
		<div class="sidebar_div_content">
			<img src="images/feed-16x16.png" /><a href="feed.php">rss 订阅</a>
		</div>
	</div>
	<!-- end rss 订阅 -->

	<!-- start 显示许可协议 -->
	<div class="sidebar_div">
		<div class="sidebar_div_title">许可协议</div>
		<div class="sidebar_div_content">
			<div style="text-align: right"><a href="root/new_post.php">_____发表新文章</a>  <a href="root/index.php">___管理</a></div><a href="http://creativecommons.org/licenses/by-nc-sa/2.5/cn/" rel="license"><img alt="Creative Commons License" style="border-width: 0pt">
</a>本<span>作品</span>采用<a href="http://creativecommons.org/licenses/by-nc-sa/2.5/cn/" rel="license">知识共享署名-非商业性使用-相同方式共享 2.5 中国大陆许可协议</a>进行许可。
		</div>
		<hr /><hr />
	</div>
	<!-- end 显示许可协议 -->
</div>
